package com.bill99.codecheck.sonar.service.impl;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import com.bill99.codecheck.sonar.orm.manager.JiraDataMng;
import com.bill99.codecheck.sonar.orm.manager.SonarMng;
import com.bill99.codecheck.sonar.service.SonarEmailService;
import com.bill99.codecheck.tools.TranslationTool;
import com.bill99.riaframework.common.dto.DataStatisticsDto;
import com.bill99.riaframework.common.dto.SonarSeriousProblemDto;

import freemarker.template.Template;

public class SonarEmailServiceImpl implements SonarEmailService {
	private SonarMng sonarMng;
	private JiraDataMng jiraMng;

	public void setJiraMng(JiraDataMng jiraMng) {
		this.jiraMng = jiraMng;
	}

	private FreeMarkerConfigurer freeMarkerConfigurer;
	private String hostUrlValue;

	public void setHostUrlValue(String hostUrlValue) {
		this.hostUrlValue = hostUrlValue;
	}

	public FreeMarkerConfigurer getFreeMarkerConfigurer() {
		return freeMarkerConfigurer;
	}

	public void setFreeMarkerConfigurer(FreeMarkerConfigurer freeMarkerConfigurer) {
		this.freeMarkerConfigurer = freeMarkerConfigurer;
	}

	public SonarMng getSonarMng() {
		return sonarMng;
	}

	public void setSonarMng(SonarMng sonarMng) {
		this.sonarMng = sonarMng;
	}

	public DataStatisticsDto getSonarEmailHtmlContent(String projectKey, Set onLineSet) throws IOException {
		Map sonarHtmlMap = sonarMng.getSonarContentByProjectKey(projectKey);
		List<SonarSeriousProblemDto> seriousList = sonarMng.getSonarSeriousProblemByProjectKey(projectKey);
		Long projectId = sonarMng.getIdByName(projectKey);
		List onLineDemanList = null;
		if (onLineSet.size() > 0) {
			onLineDemanList = jiraMng.getJiraOnlineDemonByOnLineId(onLineSet);
		}

		if (sonarHtmlMap.size() == 0 || !StringUtils.hasLength(projectId.toString())) {
			return null;
		}
		List<SonarSeriousProblemDto> seriousListCha = null;
		if (seriousList != null && seriousList.size() > 0) {
			seriousListCha = TranslationTool.transEngForCha(seriousList);
		}

		sonarHtmlMap.put("projectId", projectId.toString());
		sonarHtmlMap.put("projectKey", projectKey);

		sonarHtmlMap.put("seriousList", seriousListCha);

		sonarHtmlMap.put("sonarUrl", hostUrlValue);
		sonarHtmlMap.put("onLineDemanList", onLineDemanList);

		String templateContent = "";
		try {
			Template tpl = freeMarkerConfigurer.getConfiguration().getTemplate("SonarEmail.ftl");
			templateContent = FreeMarkerTemplateUtils.processTemplateIntoString(tpl, sonarHtmlMap);

		} catch (Exception e) {
			e.printStackTrace();
		}
		DataStatisticsDto dsd = new DataStatisticsDto();
		dsd.setEmailContent(templateContent);
		BigDecimal violationsDensity=(BigDecimal) sonarHtmlMap.get("violations_density");	
		BigDecimal blockerViolations=(BigDecimal) sonarHtmlMap.get("blocker_violations");
		BigDecimal criticalViolations=(BigDecimal) sonarHtmlMap.get("critical_violations");
		BigDecimal majorViolations=(BigDecimal) sonarHtmlMap.get("major_violations");
		BigDecimal minorViolations=(BigDecimal) sonarHtmlMap.get("minor_violations");
		BigDecimal infoViolations=(BigDecimal) sonarHtmlMap.get("info_violations");
	
//		System.out.println(violationsDensity.floatValue());
//		System.out.println(blockerViolations.intValue());
//		System.out.println(criticalViolations.intValue());
//		System.out.println(majorViolations.intValue());
//		System.out.println(minorViolations.intValue());
//		System.out.println(infoViolations.intValue());
		
		dsd.setViolationsDensity(violationsDensity.floatValue());
		dsd.setBlockerViolations(blockerViolations.intValue());
		dsd.setCriticalViolations(criticalViolations.intValue());
		dsd.setMajorViolations(majorViolations.intValue());
		dsd.setMinorViolations(minorViolations.intValue());
		dsd.setInfoViolations(infoViolations.intValue());

		return dsd;
	}

}
